var map;
var geocoder;
var address;

function initialize() {
  map = new GMap2(document.getElementById("zmap"));
  map.setCenter(new GLatLng(46.206923,6.147366), 15);
  map.addControl(new GLargeMapControl);
  map.addControl(new GLargeMapControl3D());
  map.enableScrollWheelZoom();
  map.addMapType(G_PHYSICAL_MAP);
  
  var hControl = new GHierarchicalMapTypeControl();
  hControl.addRelationship(G_SATELLITE_MAP, G_HYBRID_MAP, "Labels", false);
  map.addControl(hControl);

  // GEvent.addListener(map, "click", getAddress);
  GEvent.addListener(map, "click", getPoint);
  
}

function getPoint(overlay, latlng) {
  if (latlng) {
    address = latlng;
    map.clearOverlays();
    point = new GLatLng(address.lat() , address.lng()) ;
    marker = new GMarker(point);
    map.addOverlay(marker);
    
    // Display input
    htmlContent =
     '<b>Post a ZMS</b>' + 
     '<br>'  +
     '<b>X=</b>' + address.lat() +  '<b>,   Y=</b>' + address.lng() + '<b>,   Z=</b>' + 0.0 + 
     '<br/>' +
    '<FORM method=get action="zms-queue-in-001-servlet">' + 
    '<TABLE CLASS="zpopup" BORDER=0>' + 
    '<INPUT type=hidden name="zAuthor" value="philippe.delobel@gmail.com">' + 
    '<INPUT type=hidden name="zX" value="' + address.lat() + '">' + 
    '<INPUT type=hidden name="zY" value="' + address.lng() + '">' + 
    '<INPUT type=hidden name="zZ" value="0.0">' + 
    '<TR><TD>Recipient</TD><TD><INPUT type=text name="zRecipient"></TD></TR>' + 
    '<TR><TD>Objet</TD><TD><INPUT type=text name="zLabel" size="48" maxlength="128"></TD></TR>' + 
    '<TR><TD>Message</TD><TD><INPUT type=text name="zContent" size="48" maxlength="1024"></TD></TR>' + 
    '<TR><INPUT type="submit" value="Send it !"></TR>' + 
    '</TABLE>' +     
    '</FORM>';

    marker.openInfoWindowHtml(htmlContent);    
  }  	
  
}

function getAddress(overlay, latlng) {
  if (latlng != null) {
    address = latlng;
    geocoder = new GClientGeocoder();
    geocoder.getLocations(latlng, showAddress);
  }
}

function showAddress(response) {
  map.clearOverlays();
  if (!response || response.Status.code != 200) {
    alert("Status Code:" + response.Status.code);
  } else {
    place = response.Placemark[0];
    point = new GLatLng(place.Point.coordinates[1],
                        place.Point.coordinates[0]);
    marker = new GMarker(point);
    map.addOverlay(marker);
    marker.openInfoWindowHtml(
     '<b>Post a ZMS</b><br>'  +
    // '<b>orig latlng:</b>' + response.name + '<br/>' +
    // '<b>latlng:</b>' + place.Point.coordinates[1] + "," + place.Point.coordinates[0] + '<br>' +
    // '<b>Status Code:</b>' + response.Status.code + '<br>' +
    // '<b>Status Request:</b>' + response.Status.request + '<br>' +
    '<b>@ </b>' + place.address + ' / ' +
    '<b>' + place.AddressDetails.Country.CountryNameCode + '</b>' + 
    '<FORM method=get action="zms-queue-in-001-servlet">' + 
    '<TABLE CLASS="zpopup" BORDER=0>' + 
    '<INPUT type=hidden name="zAuthor" value="philippe.delobel@gmail.com">' + 
    '<INPUT type=hidden name="zX" value="' + place.Point.coordinates[1] + '">' + 
    '<INPUT type=hidden name="zY" value="' + place.Point.coordinates[0] + '">' + 
    '<INPUT type=hidden name="zZ" value="0.0">' + 
    '<TR><TD>Recipient</TD><TD><INPUT type=text name="zRecipient"></TD></TR>' + 
    '<TR><TD>Objet</TD><TD><INPUT type=text name="zLabel" size="48" maxlength="128"></TD></TR>' + 
    '<TR><TD>Message</TD><TD><INPUT type=text name="zContent" size="48" maxlength="1024"></TD></TR>' + 
    '<TR><INPUT type="submit" value="Send it !"></TR>' + 
    '</TABLE>' +     
    '</FORM>'

    );
  }


}
